/**
 * @created by zhangpeng18 on 2019-8-6 14:04:58
 * @updated by
 * @description ws驱动管理
 */
define(['./custom/widgets/js/yufpSelectParam.js'], function (require, exports) {
	/**
	 * 页面加载完成时触发
	 * @param hashCode 路由ID
	 * @param data 传递数据对象
	 * @param cite 页面站点信息
	 */
	exports.ready = function (hashCode, data, cite) {
		yufp.lookup.reg('PROXY_FLAG');
		yufp.custom.vue({
			el: cite.el,
			data: function () {
				var _this = this;
				/**
				 * 在 状态参数名 为空时, 返回参数名 字段为必填项
				 */
				var returnParamValidate = function (rule, value, callback) {
					if ((value === '' || value === undefined) && (_this.formdata.status === '' || _this.formdata.status === undefined)) {
						callback(new Error('状态参数名为空时这里必填'));
					} else {
						callback();
					}
				};
				/**
				 * 输入的字符串必须以 $ 开头
				 */
				var paramCodeValidate = function (rule, value, callback) {
					var reg = /^\$/;
					if (value && reg.test(value)) {
						callback();
					} else if (value && !reg.test(value)) {
						callback(new Error('请以 $ 开头'));
					} else if (value === '') {
						callback(new Error('请以 $ 开头'));
					} else {
						callback();
					}
				};
				/**
				 * 校验输入的字符串是否符合 IP地址:端口号 的格式，多个这样的字符串用英文逗号分隔
				 */
				var ipAdressPortValidate = function (rule, value, callback) {
					var reg = /^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])|[a-z|A-Z|0-9|_|-]+):([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5])$/;
					var addressArr = value.split(',');
					for (var i = 0; i < addressArr.length; ++i) {
						if (addressArr[i] && reg.test(addressArr[i])) {
							continue;
						} else if (addressArr[i] && !reg.test(addressArr[i])) {
							callback(new Error('格式为ip:port，多个ip用,分隔'));
						} else {
							continue;
						}
					}
					callback();
				};
				return {
					searchFormdata: {},
					addFnButton: !yufp.session.checkCtrl('addFn'),
					updateFnButton: !yufp.session.checkCtrl('updateFn'),
					deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
					treeDataUrl: "api/use/plats/apps/getTreeWftList",
					dataUrl: 'api/use/plats/wscfg/queryList',
					saveBtnShow: true,
					cancelBtnShow: true,
					addBtnShow: false,
					formdata: {},
					saveDisabled: false,
					async: false,
					param: { UNITID: '0000', LEVELUNIT: '1' },
					rule: [
						{ validator: returnParamValidate, trigger: 'blur' },
						{ validator: paramCodeValidate, trigger: 'blur' },
						[{ required: true, message: '必填项', trigger: 'blur' },
						{ validator: ipAdressPortValidate }]
					],
					height: yufp.frame.size().height,
					dialogVisible: false,
					classCodeDisabled: false,
					formDisabled: false,
					viewTitleArrry: ['WebService驱动新增', 'WebService驱动修改', 'WebService驱动查看'],
					viewTitle: '',
					appCodeShow: true,
					propTableData: [],
					nodeData: '', // 在新增表格数据时需要先获取当前树节点的数据，再把一条新的记录加到这个节点下
					nodeLeaf: false,
					isReadOnly: false,
					agentIds: [
					],
					nodeTemp: {}
				};
			},
			methods: {
				reloadData: function () {
					var _this = this;
					var paramObj = _this.$refs.queryModelRef.fm;
					if (_this.nodeTemp.appCode != null && _this.nodeTemp.appCode != '0000') {
						paramObj.appCode = _this.nodeTemp.appCode;
					}
					_this.$refs.refTable.remoteData(paramObj);
				},

				/**
				 * 获取参数列表子组件选中的行数据
				 */
				getSelectedData: function (data) {
					var _this = this;
					_this.$refs.refPropTable.selections[0].paramValue = '{#' + data[0].paramValue + '#}';
				},
				/**
				 * 取消
				 */
				cancelFn: function () {
					var _this = this;
					_this.dialogVisible = false;
				},
				/**
				 * 保存
				 */
				saveFn: function () {
					var _this = this;
					var validate = false;
					_this.$refs.refForm.validate(function (valid) {
						validate = valid;
					});
					if (!validate) {
						return;
					}

					_this.checkClassCode(validate);

					if (!validate) {
						return;
					}

					var paramObj = _this.formdata;


					if (paramObj.useParamValues != null) {
						paramObj.useParamValues.splice(0);
					} else {
						paramObj.useParamValues = new Array();
					}

					for (let i = 0; i < _this.propTableData.length; i++) {
						paramObj.useParamValues.push(_this.propTableData[i]);
					}

					let urlparam = "";
					if (_this.addFlag) {
						paramObj.appCode = _this.nodeTemp.appCode;
						urlparam = 'api/use/plats/wscfg/saveUseWsCfgPo';
					} else {
						urlparam = 'api/use/plats/wscfg/updateUseWsCfgPo';
					}


					yufp.service.request({
						method: 'POST',
						url: urlparam,
						data: paramObj,
						callback: function (code, message, response) {

							if (response.code == 0) {
								_this.reloadData();
								_this.$message('操作成功');
								_this.dialogVisible = false;
							} else {
								_this.$message({ message: response.message, type: "warning" });
							}
						}
					});

				},
				/**
				 * 控制保存按钮、xdialog、表单的状态
				 * @param viewType 表单类型
				 * @param editable 可编辑,默认false
				 */
				switchStatus: function (viewType, editable) {
					var _this = this;
					_this.viewType = viewType;
					_this.dialogVisible = true;
					_this.formDisabled = !editable;
					_this.saveBtnShow = editable;
					_this.cancelBtnShow = editable;
				},
				/**
				 * 获取参数表数据
				 */
				getPropTableData: function (data, total) {
					var _this = this;
					_this.propTableData.splice(0);
					for (var i = 0; i < data.length; i++) {
						_this.propTableData.push(data[i]);
					}
				},
				/**
				 * 新增参数
				 */
				propAddFn: function () {
					var _this = this;
					var obj = {
						paramCode: '',
						paramName: '',
						paramValue: ''
					};
					_this.propTableData.push(obj);
				},
				/**
				 * 删除参数
				 */
				propDeleteFN: function () {
					var _this = this;
					let selectDatas = _this.$refs.refPropTable.selections;
					for (let jdx = _this.propTableData.length - 1; jdx >= 0; jdx--) {
						for (let idx = 0; selectDatas.length > idx; idx++) {
							if (_this.propTableData[jdx] == selectDatas[idx]) {
								_this.propTableData.splice(jdx, 1);
								break;
							}
						}

					}
				},
				/**
				 * 点击树的结点，加载对应的数据
				 */
				nodeClickFn: function (nodeData, node, self) {
					var _this = this;
					if (nodeData.appCode != '0000') {
						_this.nodeLeaf = true;
						_this.nodeTemp.appCode = nodeData.appCode;
						_this.$refs.refTable.remoteData({ appCode: _this.nodeTemp.appCode });

					} else {
						_this.nodeLeaf = false;
						_this.nodeTemp.appCode = null;
						_this.$refs.refTable.remoteData({ appCode: "" });
					}
				},
				/**
				 * 新增
				 */
				addFn: function () {
					var _this = this;
					_this.isReadOnly = false;
					_this.dialogVisible = true;
					_this.viewTitle = _this.viewTitleArrry[0];
					_this.addFlag = true;
					_this.saveDisabled = false;
					_this.getAgentIds(_this.nodeTemp.appCode);

					_this.$nextTick(function () {
						_this.viewTitle = _this.viewTitleArrry[0];
						_this.$refs.refForm.resetFields();
						_this.propTableData.splice(0);
						_this.formdata.errorReturn = '2'; // 默认初始值
					});
				},
				/**
				 * 修改
				 */
				modifyFn: function () {
					var _this = this;
					_this.saveDisabled = false;
					if (_this.$refs.refTable.selections.length != 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning',duration: 6000 });
						return;
					}
					_this.isReadOnly = true;
					_this.nodeTemp.appCode = _this.$refs.refTable.selections[0].appCode;
					_this.getAgentIds(_this.$refs.refTable.selections[0].appCode);
					_this.viewTitle = _this.viewTitleArrry[1];
					_this.dialogVisible = true;
					_this.addFlag = false;
					_this.refTemp = _this.$refs.refTable.selections[0];
					var data = _this.$refs.refTable.selections[0].useParamValues;

					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						_this.propTableData.splice(0);
						yufp.clone(_this.refTemp, _this.formdata);
						if (data != null)
							for (var i = 0; i < data.length; i++) {
								_this.propTableData.push(data[i]);
							}
					});
				},

				viewFn: function (obj) {

					var _this = this;
					_this.saveDisabled = true;
					_this.nodeTemp.appCode = obj.appCode;
					_this.isReadOnly = true;
					_this.getAgentIds(obj.appCode);
					_this.viewTitle = _this.viewTitleArrry[2];
					_this.dialogVisible = true;
					_this.addFlag = false;
					_this.refTemp = obj;
					var data = obj.useParamValues;

					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						_this.propTableData.splice(0);
						yufp.clone(_this.refTemp, _this.formdata);
						if (data != null)
							for (var i = 0; i < data.length; i++) {
								_this.propTableData.push(data[i]);
							}
					});

				},

				/**
				 * 删除
				 */
				deleteFn: function () {
					var _this = this;
					var selections = _this.$refs.refTable.selections;
					if (selections.length < 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning',duration: 6000 });
						return;
					}
					var len = selections.length, arr = [];
					for (var i = 0; i < len; i++) {
						arr.push(selections[i].id);
					}
					_this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',duration: 6000,
						center: true,
						callback: function (action) {
							if (action === 'confirm') {
								yufp.service.request({
									method: 'DELETE',
									url: "api/use/plats/wscfg/deleteUseWsCfgPo",
									data: selections[0],
									callback: function (code, message, response) {
										if (response.code != 0) {
											isOk = false;
											_this.$message(response.message);
										} else {
											_this.$message('操作成功');
											_this.reloadData();

										}

									}
								});
							}
						}
					});
				},
				getAgentIds: function (appCode) {

					var _this = this;
					_this.agentIds.splice(0);
					const model = {};
					model.appCode = appCode;

					var params = { condition: JSON.stringify(model) };

					var initObj = {};
					initObj.key = "_0";
					initObj.value = "虚拟代理";
					_this.agentIds.push(initObj);
					yufp.service.request({
						method: 'GET',
						url: "api/use/plats/apps/queryWftAppList",
						data: params,
						async: false,
						callback: function (code, message, response) {

							if (response.data.length > 0) {
								const wftAgtList = response.data[0].wftAgtList;

								if (wftAgtList != null)
									for (let j = 0; j < wftAgtList.length; j++) {
										var obj = {};
										obj.key = wftAgtList[j].agentId;
										obj.value = wftAgtList[j].agentName;
										_this.agentIds.push(obj);

									}
							}
						}
					});

				},
				checkClassCode: function (isOk) {

					var _this = this;
					var paramObj = _this.formdata;
					paramObj.useParamValues = [];
					if (!_this.addFlag) {
						return;
					}

					yufp.service.request({
						method: 'GET',
						url: "api/use/plats/dscfg/checkClassCode",
						data: paramObj,
						async: false,
						callback: function (code, message, response) {
							if (response.code != 0) {
								isOk = false;
								_this.$message(response.message);
							} else {
								if (!response.data) {
									isOk = false;
									_this.$message('该驱动缩写在系统中已经存在');
								}
							}

						}
					});
				},

			}
		});
	};
});